#include "Control_Body.h"

#include "Control_Menu.h"
#include "Host.h"

#include "Rect.h"

using namespace gui;

Control_Body::Control_Body(Vec<2, float> NewPos, Vec<2, float> NewSize, Control_Menu* NewParentMenu, Host& NewParentHost) :
Control_Base(NewParentMenu, NewParentHost),
Pos(NewPos),
Size(NewSize),
MouseAbove(false),
ParentMenuPos(nullptr),
ParentMenuLayer(nullptr)
{
if (NewParentMenu != nullptr)
{
ParentMenuLayer = &NewParentMenu->Layer;
ParentMenuPos = &NewParentMenu->Pos;
}
}


Control_Body::~Control_Body()
{
}

bool Control_Body::CheckIntersection()
{
	bool Intersects;
	if (ParentMenu)
	{
		const Rect<float> BodyRect = Rect<float>(Pos.X, Pos.Y, Pos.X + Size.X, Pos.Y + Size.Y);
		Intersects = BodyRect.Contains(ParentHost.Status.MousePos - ParentMenu->Pos);
	}
	else
	{
		const Rect<float> BodyRect = Rect<float>(Pos.X, Pos.Y, Pos.X + Size.X, Pos.Y + Size.Y);
		Intersects = BodyRect.Contains(ParentHost.Status.MousePos);
	}

	if (MouseAbove != Intersects)
	{
		MouseAbove = Intersects;

		if (MouseAbove)
			MouseIntersectionBegin();
		else
			MouseIntersectionEnd();
	}

	return Intersects;
}

void Control_Body::HostVertexFixModification()
{
	ParentHost.VerticesFixChanged = true;
};


void Control_Body::HostCloseParentMenu()
{
	ParentHost.CloseMenu(ParentMenu->Name);
}